package com.neusoft.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.bean.NewPageBean;
import com.neusoft.bean.PageBean;
import com.neusoft.bean.PageRequestDto;
import com.neusoft.bean.RegisterBean;
import com.neusoft.mapper.RegisterMapper;
import com.neusoft.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public String getMaxCaseNumber() {
        return registerMapper.selectMaxCaseNumber();
    }

    @Override
    public Integer getDoctorUsedNumber(Integer doctorId) {
        return registerMapper.selectCountByEmployeeId(doctorId);
    }

    @Override
    public String saveRegister(Map<String, Object> register) {
        register.put("visit_state",1);
        Boolean is_book=(Boolean) register.get("is_book");
        if (is_book){
            register.put("is_book","是");
        }else {
            register.put("is_book","否");
        }
        String birthday=(String) register.get("birthday");
        birthday=birthday.substring(0,birthday.indexOf("T"));
        register.put("birthday",birthday);

        String visit_date=(String) register.get("visit_date");
        visit_date=visit_date.replace("T"," ").replace("Z"," ");
        register.put("visit_date",visit_date);


        Integer count= registerMapper.insertRegister(register);
        return count==1? "挂号成功":"挂号失败";
    }

    @Override
    public Integer getFinishPatientCount(Integer employeeId) {
        return registerMapper.selectCountByEmployeeIdAndVS(employeeId);
    }

    @Override
    public Integer getWaitPatientCount(Integer employeeId) {
        return registerMapper.selectWaitCountByEmployeeIdAndVS(employeeId);
    }

    @Override
    public PageBean getWaitPatient(Integer page, Integer pageSize, Integer employeeId, String caseNumber, String realName) {
        //指定当前页和每页显示条数
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo=registerMapper.selectWaitPatientByPage(employeeId,caseNumber,realName);
        PageBean pageBean=new PageBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;

    }

    @Override
    public NewPageBean getWaitCheckPatient(PageRequestDto pageRequestDto) {
        PageHelper.startPage(pageRequestDto.getPage(), 5);
        NewPageBean newPageBean= new NewPageBean();
        List<RegisterBean> rows = new ArrayList<>();
        for (int i = 0; i <pageRequestDto.getRegisterIdList().size(); i++) {
            RegisterBean registerBean=registerMapper.selectWaitCheckPatient(pageRequestDto.getRegisterIdList().get(i), pageRequestDto.getCaseNumber(), pageRequestDto.getRealName());
            if (registerBean != null) {
                rows.add(registerBean);
            }
        }
        PageInfo<RegisterBean> pageInfo = new PageInfo<>(rows);
        // 创建 PageBean 对象并设置数据
        newPageBean.setRows(rows);
        newPageBean.setCount(pageInfo.getTotal());
        newPageBean.setMsg("success");
        newPageBean.setCode("200");

        return newPageBean;
    }
    @Override
    public PageBean getPatient(Integer page, Integer pageSize, String caseNumber, String realName) {
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo = registerMapper.selectPatientPage(caseNumber, realName);
        PageBean pageBean = new PageBean();
        List<Map<String, Object>> collect = pageInfo.getResult().stream().map(map -> {
            Object visitState = map.get("visit_state");
            if (Integer.parseInt(visitState.toString()) == 1) {
                map.put("visitStateName", "已挂号");
            } else if (Integer.parseInt(visitState.toString()) == 2) {
                map.put("visitStateName", "医生接诊");
            } else if (Integer.parseInt(visitState.toString()) == 3) {
                map.put("visitStateName", "看诊结束");
            }else if (Integer.parseInt(visitState.toString()) == 4) {
                map.put("visitStateName", "已退号");
            }
            LocalDateTime visitDate = (LocalDateTime)map.get("visit_date");
            map.put("visit_date", visitDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            return map;
        }).collect(Collectors.toList());
        pageBean.setRows(collect);
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }

    @Override
    public PageBean getAllPatient(Integer page, Integer pageSize, String caseNumber, String realName) {
        //指定当前页和每页显示条数
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo=registerMapper.selectAllPatientByPage(caseNumber,realName);
        PageBean pageBean=new PageBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }

    @Override
    public void changeVisitState(Integer caseNumber) {
        registerMapper.changeVisitState(caseNumber);
    }


    //根据姓名和病历号查询信息
    @Override
    public List<Map<String, Object>> searchPatientInformation(String caseNumber, String realName ) {
        return registerMapper.selectPatient(caseNumber, realName);
    }

    @Override
    public NewPageBean getWaitInpectionPatient(PageRequestDto pageRequestDto) {
        PageHelper.startPage(pageRequestDto.getPage(), 5);
        NewPageBean newPageBean= new NewPageBean();
        List<RegisterBean> rows = new ArrayList<>();
        for (int i = 0; i <pageRequestDto.getRegisterIdList().size(); i++) {
            RegisterBean registerBean=registerMapper.selectWaitInspectionPatient(pageRequestDto.getRegisterIdList().get(i), pageRequestDto.getCaseNumber(), pageRequestDto.getRealName());
            if (registerBean != null) {
                rows.add(registerBean);
            }
        }
        PageInfo<RegisterBean> pageInfo = new PageInfo<>(rows);
        // 创建 PageBean 对象并设置数据
        newPageBean.setRows(rows);
        newPageBean.setCount(pageInfo.getTotal());
        newPageBean.setMsg("success");
        newPageBean.setCode("200");

        return newPageBean;
    }

    @Override
    public NewPageBean getWaitDisposalPatient(PageRequestDto pageRequestDto) {
        PageHelper.startPage(pageRequestDto.getPage(), 5);
        NewPageBean newPageBean= new NewPageBean();
        List<RegisterBean> rows = new ArrayList<>();
        for (int i = 0; i <pageRequestDto.getRegisterIdList().size(); i++) {
            RegisterBean registerBean=registerMapper.selectWaitDisposalPatient(pageRequestDto.getRegisterIdList().get(i), pageRequestDto.getCaseNumber(), pageRequestDto.getRealName());
            if (registerBean != null) {
                rows.add(registerBean);
            }
        }
        PageInfo<RegisterBean> pageInfo = new PageInfo<>(rows);
        // 创建 PageBean 对象并设置数据
        newPageBean.setRows(rows);
        newPageBean.setCount(pageInfo.getTotal());
        newPageBean.setMsg("success");
        newPageBean.setCode("200");

        return newPageBean;
    }

    @Override
    public PageBean searchDisPatient(Integer page, Integer pageSize, String caseNumber, String realName) {
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo=registerMapper.selectDisposalPatientByPage(caseNumber,realName);
        PageBean pageBean=new PageBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }
    //分页查询患者信息
    @Override
    public PageBean checkPatient(Integer page, Integer pageSize, String caseNumber, String realName) {
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo=registerMapper.selectPatientByPage(caseNumber,realName);
        PageBean pageBean=new PageBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }
    @Override
    public PageBean searchPatient(Integer page, Integer pageSize, String caseNumber, String realName) {
        //指定当前页和每页显示条数
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo=registerMapper.selectSearchPatientByPage(caseNumber,realName);
        PageBean pageBean=new PageBean();
        pageBean.setRows(pageInfo.getResult());
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;

    }
//    y
    @Override
    public List<Map<String, Object>> getWaitCheck(String realName, String caseNumber) {
        return registerMapper.selectCheckById(realName,caseNumber);
    }

}
